package bit_operation;

public class missingTwo {
    public int[] missingTwo(int[] nums) {
        //1.先把所有数字异或到一起
        int tmp = 0;
        for(int x : nums) {
            tmp ^= x;
        }
        for(int i = 1;i< nums.length+2;i++) {
            tmp ^= i;
        }
        //2.找出 a，b 两个数比特位不同的那一位
        int diff = 0;
        while (true) {
            if( ((tmp >> diff) & 1) == 1) break;
            else diff++;
        }
        //3.将所有的数按照 diff 位不同，分成两类异或
        int[] ret = new int[2];
        for(int x : nums){
            if(((x>>diff) & 1)==1) {
                ret[1] ^= x;
            }else {
                ret[0] ^= x;
            }
        }
        for(int i = 1;i <= nums.length+2;i++) {
            if( ((i>>diff) & 1) == 1) ret[1] ^= i;
            else ret[0] ^= i;
        }
        return ret;

    }
}